/**
 * Created by wangteng on 2018/8/20.
 */
'use strict';
cBoard.service('chartThreeBarService', function ($state, $window) {
    // 负责调用对应的Render渲染图表
    this.render = function (containerDom, option, scope, persist, drill, relations, chartConfig) {
        var render = new CBoardHighChartRender(containerDom, option);
    };
    // 负责渲染图表需要的配置（包括数据结构）
    this.parseOption = function (data) {
        var chartConfig = data.chartConfig;
        var casted_keys = data.keys;
        var casted_values = data.series;
        var aggregate_data = data.data;
        var newValuesConfig = data.seriesConfig;
        var series_data = [];
        var string_keys = _.map(casted_keys, function (key) {
            return key.join('-');
        });
        var tunningOpt = chartConfig.option;
        var zipDataWithCfg = _.chain(aggregate_data)
                    .map(function (data, i) {
                        var joined_values = casted_values[i].join('-');
                        var s = newValuesConfig[joined_values];
                        s.key =joined_values;
                        s.data = data;
                        return s;
            }).value()


        for (var j = 0; aggregate_data[0] && j < aggregate_data[0].length; j++) {
            for (var i = 0; i < aggregate_data.length; i++) {
                aggregate_data[i][j] = aggregate_data[i][j] ? Number(aggregate_data[i][j]) : 0;
            }
        }

        for (var i = 0; i < zipDataWithCfg.length; i++) {
            var s = zipDataWithCfg[i];
            s.name = s.key;
            s.coordinateSystem = chartConfig.coordinateSystem;
            series_data.push(s);

            console.log( series_data);
        }
        var highChartOption = {
            chart: {
                type: 'column',
                options3d: {
                    enabled: true,
                    alpha: 5,
                    beta: 5,
                    depth: 40,
                    viewDistance: 25
                },
            },
            colors: ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9',
                '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'],
            plotOptions: {
                column: {
                    depth: 25
                }
            },
            xAxis: {
                title: {
                    enabled: true,
                },
                categories: casted_keys
            },
            yAxis: {
                title: {
                    enabled: true,
                },
            },
            legend: {
                enabled: false,
                verticalAlign: 'top',
            },
             series: series_data
         /*   series: [{
                name:series_data[0].name,
                data:series_data[0].data
            }]*/

        };
        // Apply tunning options
         updateHighchartOptions(tunningOpt, highChartOption);
        return highChartOption;


    };

});
